// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI);

import axios from 'axios';
import qs from 'qs';

Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;

Vue.prototype.HOME = '/api';


Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>'
});
/**
 * 路由拦截
 * */
const whiteList = ['/'];  // 免登录白名单

router.beforeEach((to, from, next) => {
  if (sessionStorage.getItem('state') === "success") { // determine if there has token
    /* has token*/
    if (to.path === '/') {
      next({ path: '/homePage' })
    }
    next()
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      next('/') // 否则全部重定向到登录页
    }
  }
});



// router.beforeEach((to, from, next) => {
//   if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
//     console.log("不用验证");
//     if (sessionStorage.getItem('state') === "success") { // 查询本地存储信息是否已经登陆
//       next();
//       console.log("验证通过");
//     } else {
//       console.log("验证不通过");
//       next({
//         path: '/', // 未登录则跳转至login页面
//         query: {redirect: to.fullPath} // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
//       });
//     }
//   } else {
//     next();
//   }
// });


// const whiteList=['/login'];//不需要登录能访问的path
// router.beforeEach((to, from, next) => {
//   console.log('beforeEach');
//   var userInfo= JSON.parse(sessionStorage.getItem('state'));//获取缓存看是否登录过
//   if(whiteList.indexOf(to.path)<0){//访问了需要登录才能访问的页面
//     if(userInfo){//登录过来直接进去
//       next();
//     }else{
//       if(to.path=='/login'){
//         next();
//       }else{
//         next('/login');
//       }
//     }
//   }
//   else{
//     next();
//   }
// });
